//
//  MenuView.m
//  网易新闻
//
//  Created by dllo on 15/10/10.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "MenuView.h"
#import <ShareSDK/ShareSDK.h>
#import "Settings.h"
@interface MenuView()
@property(nonatomic, retain)UIView *contentView;
@property(nonatomic, retain)UIButton *background;
@property(nonatomic, retain)UIButton *collection;
@end
@implementation MenuView
- (instancetype)init{
    self = [super init];
    if (self) {
        self.hidden = YES;
        [self setUserInteractionEnabled:NO];
        
        self.background = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.background addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.background];
        
        self.contentView = [[UIView alloc]init];
        self.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];;
        self.contentView.alpha = 0.9;
        self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;
        self.contentView.layer.shadowOffset = CGSizeMake(2,2);
        self.contentView.layer.shadowOpacity = 0.8;
        self.contentView.layer.shadowRadius = 4;
        [self createContentView];
        [self.background addSubview:self.contentView];
        [self.contentView release];
    }
    return self;
}

- (void)createContentView{
    self.contentView.frame = CGRectMake(280, 45, 80, 120);
    
    self.collection = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collection.frame = CGRectMake(0, 0, 80, 40);
    [self.collection setTitle:@"收藏" forState:UIControlStateNormal];
    [self.collection setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.collection addTarget:self action:@selector(collection:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.collection];
    
    UIButton *share = [UIButton buttonWithType:UIButtonTypeSystem];
    share.frame = CGRectMake(0, 40, 80, 40);
    [share setTitle:@"分享" forState:UIControlStateNormal];
    [share setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [share addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:share];
    
    UIButton *night = [UIButton buttonWithType:UIButtonTypeSystem];
    night.frame = CGRectMake(0, 80, 80, 40);
    [self setNightTitle:night];
    [night setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [night addTarget:self action:@selector(night:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:night];
}

- (void)popView{
    if (self.hidden) {
        if ([[Settings shareSettings]checkDocid:self.docid]) {
            [self.collection setTitle:@"已收藏" forState:UIControlStateNormal];
            [self.collection setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        self.hidden = NO;
        [self.superview bringSubviewToFront:self];
        self.frame = CGRectMake(95, -165, 375, 645);
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, 20, 375, 645);
            self.background.frame = self.frame;
        }];
        [self setUserInteractionEnabled:YES];
    }
}

- (void)closeView:(UIButton *)button{
    [self setUserInteractionEnabled:NO];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(95, -165, 375, 645);
        self.background.frame = self.frame;
    }completion:^(BOOL finished) {
        if (finished) {
            self.hidden = YES;
        }
    }];
}

- (void)collection:(UIButton *)button{
    if (![[Settings shareSettings]checkDocid:self.docid]) {
        NSDate *senddate = [NSDate date];
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat: @"YY-MM-dd"];
        NSString *ptime = [dateformatter stringFromDate:senddate];
        [dateformatter release];
        
        NSMutableArray *muArr = [[NSMutableArray alloc]initWithArray:[[Settings shareSettings]collectionsArr]];
        [muArr addObject:@{@"docid":self.docid, @"title":self.mod.title, @"ptime":ptime}];
        [[Settings shareSettings]changeCollectionArr:muArr];
        [muArr release];
    }
    [self closeView:button];
}

- (void)share:(UIButton *)button{
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:self.mod.body images:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"fenxiang" ofType:@"jpg"]] url:[NSURL URLWithString:[NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.docid]] title:self.mod.title type:SSDKContentTypeImage];
    [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
            }
                break;
            case SSDKResponseStateFail:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alertView show];
            }
                break;
            case SSDKResponseStateCancel:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"取消分享" message: nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alertView show];
            }
                break;
            default:
                break;
        }
    }];
    [self closeView:button];
}

- (void)night:(UIButton *)button{
    [Settings shareSettings].isNight = ![Settings shareSettings].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeBackgroundColor" object:nil userInfo:nil];
    [self setNightTitle:button];
    [self closeView:button];
}

- (void)setNightTitle:(UIButton *)button{
    if ([Settings shareSettings].isNight) {
        [button setTitle:@"日间模式" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"夜间模式" forState:UIControlStateNormal];
    }
}
- (void)dealloc{
    [_contentView release];
    [super dealloc];
}
@end
